<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">

      <uni-forms-item name="category_id" label="案例类别" required>
        <uni-data-picker v-model="formData.category_id" collection="zhimuta-tabsList"
          field="name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="title" label="标题" required>
        <uni-easyinput placeholder="标题" v-model="formData.title" autoHeight type="textarea" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="intro" label="案例简介" required>
        <uni-easyinput placeholder="案例简介" v-model="formData.intro" autoHeight type="textarea"
          trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="article_status" label="案例状态" required>
        <uni-data-checkbox v-model="formData.article_status"
          :localdata="formOptions.article_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="view_count" label="阅读数量">
        <uni-easyinput placeholder="阅读数量" type="number" v-model="formData.view_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="collect_count" label="收藏人数">
        <uni-easyinput placeholder="收藏人数" type="number" v-model="formData.collect_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="avatar" label="封面" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.avatar"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="author" label="作者名字" required>
        <uni-easyinput placeholder="作者名字" v-model="formData.author" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="au_avatar" label="作者头像" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.au_avatar"></uni-file-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import {
    validator
  } from '../../js_sdk/validator/zhumuta-cash-info.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'zhumuta-cash-info';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }



  export default {
    data() {
      let formData = {
        "category_id": "",
        "title": "",
        "intro": "",
        "article_status": 0,
        "view_count": 0,
        "collect_count": 0,
        "avatar": null,
        "author": "",
        "au_avatar": null
      }
      return {
        formData,
        formOptions: {
          "article_status_localdata": [{
              "value": 0,
              "text": "未发布"
            },
            {
              "value": 1,
              "text": "已发布"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {

      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {}).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>